import {BaseCrudApi} from "~/api/util/BaseService";
import { Company, SettingType } from "@/api/system/company/types";
import { JsonResult } from "~/api/types";
import { ImageApi } from "~/api/system/file/image.api";

export class CompanyApi extends BaseCrudApi<Company> {

    baseUrl: string = "/system/company";

  getCurrentDetail() {
    this.detailState.form = {};
    this.beforeDetail(this.detailState);
    this.detailState.visible = true;
    this.detailState.loading = true;
    this.get("/getCurrentDetail").then((res) => {
      this.detailState.form = res.data as any;
    }).catch((reason: any) => {
      console.log(reason);
    }).finally(() => {
      this.detailState.loading = false;
      this.afterDetail(this.detailState);
    });
  }

  updateSetting(id: string, setting: SettingType) {
    let settingList = [];
    settingList.push({
      type: "HEADER",
      label: "标题",
      value: setting.header.title,
      code: "title",
    });
    settingList.push({
      type: "HEADER",
      label: "logo",
      value: setting.header.logo,
      code: "logo",
    });
    settingList.push({
      type: "APPLET",
      label: "小程序appid",
      value: setting.applet.appid,
      code: "appid",
    });
    settingList.push({
      type: "APPLET",
      label: "小程序秘钥",
      value: setting.applet.secret,
      code: "secret",
    });
    settingList.push({
      type: "GENERAL",
      label: "公众号appid",
      value: setting.general.appid,
      code: "appid",
    });
    settingList.push({
      type: "GENERAL",
      label: "公众号秘钥",
      value: setting.general.secret,
      code: "secret",
    });
    settingList.push({
      type: "GENERAL",
      label: "认证的token",
      value: setting.general.token,
      code: "token",
    });
    settingList.push({
      type: "GENERAL",
      label: "认证的token秘钥",
      value: setting.general.aesKey,
      code: "aesKey",
    });
    setting.samplePlace.forEach(item => {
      settingList.push({
        type: "SAMPLE_PLACE",
        label: item.label,
        value: item.value,
      });
    });
    setting.varietySetting.forEach(item => {
      settingList.push({
        type: "VARIETY_FRUITS",
        label: item.label,
        value: item.value,
      });
    });
    this.update(id, {
      settingList: settingList,
    }).then(res => {
    });
  }

  async getSetting() {
    const res: JsonResult<Company> = await this.get("/getCurrentDetail");
    let settings: SettingType = { applet: { appid: "", secret: "" }, general: { aesKey: "", appid: "", secret: "", token: "" }, header: { logo: "", title: "" }, samplePlace: [], varietySetting: [] };
    if (res.ok) {
      const imageApi = new ImageApi();
      res.data?.settingList?.forEach(item => {
        if (item.type == "HEADER") {
          if (item.code == "title") {
            settings.header.title = item.value as any;
          }
          if (item.code == "logo") {
            settings.header.logo = item.value as any;
            imageApi.getImg(item.value as any).then(src => {
              // @ts-ignore
              imageApi.img.src = src;
              imageApi.img.value = item.value as any;
            });
          }
        }
        if (item.type == "APPLET") {
          if (item.code == "appid") {
            settings.applet.appid = item.value as any;
          }
          if (item.code == "secret") {
            settings.applet.secret = item.value as any;
          }
        }
        if (item.type == "GENERAL") {
          if (item.code == "appid") {
            settings.general.appid = item.value as any;
          }
          if (item.code == "secret") {
            settings.general.secret = item.value as any;
          }
          if (item.code == "token") {
            settings.general.token = item.value as any;
          }
          if (item.code == "aesKey") {
            settings.general.aesKey = item.value as any;
          }
        }
        if (item.type == "SAMPLE_PLACE") {
          settings.samplePlace.push({
            value: item.value,
            label: item.label,
          });
        }
        if (item.type == "VARIETY_FRUITS") {
          settings.varietySetting.push({
            value: item.value,
            label: item.label,
          });
        }
      });

      let data: { id: string, settings: SettingType } = {
        id: res.data?.id as string,
        settings: settings,
      };
      return data;
    }
  }
}
